package ca.cbc.msb.mq;

import ca.cbc.medialib.util.ApplicationContextUtil;
import ca.cbc.msb.entity.MSBMessage;
import ca.cbc.msb.persistence.ProcessingStatusPersistence;
import ca.cbc.msb.util.ProgressReport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.jms.JMSException;
import javax.naming.NamingException;

/**
 * @author : Arash Bizhanzadeh <arash.bizhanzadeh@cbc.ca>
 *         Date: 11-09-26
 *         Time: 10:26 AM
 *         CBC.ca All rights reserved.
 */
public class VerificationFailedMessageHandler implements MessageHandler{
    private static Logger logger = LoggerFactory.getLogger(VerificationFailedMessageHandler.class);

    private ProcessingStatusPersistence processingStatusPersistence;

    public VerificationFailedMessageHandler() throws NamingException, JMSException {
    	processingStatusPersistence = ApplicationContextUtil.instance.getApplicationContext().getBean(ProcessingStatusPersistence.class);  	          
    }
    
    @Override
    public void handle(MSBMessage message, MessageUtil messageUtil) throws NamingException, JMSException {
        logger.debug("Handling Verification failed for {}", message.getMedia().toString());
        //Submission to yospace timed out we need to clean the system.
    	messageUtil.commit();
        processingStatusPersistence.removeProcessingFlag(message);
        ProgressReport.instance.statusReport(message, "Error in deleting the timed-out process from database, process table left in an unstable condition");
    }
}
